/**
 * @author sce
 * @description 获取用户登录信息，同时更新token有效时间
 */

import { BaseContext } from 'koa';
import session from './../service/session';

export default () => {
    return async (ctx: BaseContext, next: any) => {
        if (!ctx.headers.token) {
            ctx.user = 'no';
        } else {
            const _user = await session.query({ key: ctx.headers.token });
            if (!_user && !_user.id) {
                ctx.user = 'expired';
            } else {
                ctx.user = _user;
                await session.create({
                    key: ctx.headers.token,
                    value: _user
                });
            }
        }
        await next();
    };
};